<?php

  require("../configuracion.php");
   $idSysSection = $sectionSegmentacion;
  require("../application-top.php");


  $SelectedOpt = explode(",", $_POST['SelectedOpt']);
  require ("segmento-tablas.php");
  require ("segmento-info.php");
?>

<html>
<head>
        <title><?= $instanciaECRM ?></title>
        <link rel="stylesheet" href="../styles/style.css" type="text/css">
        <script type="text/javascript" src="../scripts/dynlayer.js"></script>
</head>
<body leftmargin="0" class="mainBackground" topmargin="0" marginwidth="0" marginheight="0">
<form name="forma" action="segmento-nuevo4.php?idSegmento=1" method="POST">

<table width="100" border="0" cellpadding="0" cellspacing="0">
  <tr><td><img src="../images/headerBar.gif" width="590" height="60" alt=""></td></tr>
  <tr>
    <td><table width="800" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#FFFFFF" valign="top" height="400">


<table border="0" cellpadding="5" cellspacing="0" width="750">
  <tr><td colspan="2"><table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr><td colspan="2"><img src="../images/spacer.gif" height="10" width="1" alt=""></td></tr><tr><td><strong class="pageTitle">Segmentaci&oacute;n</strong><br>
<?php
 $ids2 = $_GET['idSegmento'];  //si es peticion para hacer cambios sobre uno ya hecho
  if ($ids2 > 0) {    
   print "<strong class=\"pageSubtitle\">Modificar segmento</strong>";
  } else {
   print "<strong class=\"pageSubtitle\">Nuevo segmento</strong>";
  }
?> 
    </td><td align="right"><a href="http://www.cacto.com/" target="_blank"><img src="../images/trademarkCacto.gif" alt="Cacto" border="0"></a><br>
<strong class="systemTitle"><?= $instanciaECRM ?></strong><br>Perfil <strong><?= $PerfilUsuario ?></strong></td></tr>
  </table></td></tr>
<!--
        <tr><td colspan="2">
                <table border="0" cellpadding="5" cellspacing="1" class="mainTableBG">
                        <tr class="answerCellBG">
                                <td><img src="../images/icArrowGo.gif" height="13" width="13" alt=""></td>
                        <td><a href="javascript:history.back();">Regresar</a></td>
                        </tr>
                </table>
        </td></tr>
//-->
        <tr>
        <td valign="top" width="100%">
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr><td>
                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr><td colspan="2">
                                <table border="0" cellpadding="5" cellspacing="1" class="mainTableBG">
                                        <tr><td colspan="2" align="right">
                                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                                        <tr>
                                                                <td class="secTableTitle"><strong class="mainTableTitle">Segmento</strong>
																</td>
                                                                <td class="secTableTitle" align="right">Paso 4 de 5</td>
                                                        </tr>
                                                </table>
                                        </td></tr>
                                        <tr class="answerCellBG"><td><strong>Paso 4</strong>. Verifica si las reglas del segmento est&aacute;n bien definidas y van de acuerdo a lo que necesitas</td></tr>
                                        <tr class="answerCellBG"><td>


<?php


  $RestriccTabla = $_POST['RestriccTabla'];
  $RestriccCampo = $_POST['RestriccCampo'];
  $RestriccOperArit = $_POST['RestriccOperArit'];
  $RestriccValor = $_POST['RestriccValor'];
  $RestriccOperLogico = $_POST['RestriccOperLogico'];


  /* inicio de Obtenenos los datos de las encuesta que se escogieron */
  foreach ($SelectedOpt as $_t) {
    if (strpos ($_t, "Encuestas#") > 0){
      $id_Encuesta = substr($_t, (strpos ($_t, "#") + 1));
      $campos = datos_encuestas($id_Encuesta);
      $CamposID[$_t] = explode(",", $campos[0]);
      $CamposType[$_t] = explode(",", $campos[1]);
      $CamposDesc[$_t] = explode(",", $campos[2]);
    }
  }
  /* fin de Obtenenos los datos de las encuesta que se escogieron */



  //Guardar las variables codificadas para pasarlas entre las paginas y gardarlas en la DB
  for ($i = 0; $i < count($RestriccTabla); $i++) {
    $RestriccTablaStr[$i] = $RestriccTabla[$i];
    $RestriccCampoStr[$i] = $RestriccCampo[$i];
  }
  
  $ids = $_POST['idSegmento'];  //si es peticion para hacer cambios sobre uno ya hecho
  if ($ids > 0) {
    print "<input type=\"hidden\" name=\"idSegmento\" value=\"$ids\">";
  }
  
  print "<input type=\"hidden\" name=\"RestriccTablaStr\" value=\"" . htmlentities(urlencode(serialize($RestriccTablaStr))) . "\">\n";
  print "<input type=\"hidden\" name=\"RestriccCampoStr\" value=\"" . htmlentities(urlencode(serialize($RestriccCampoStr))) . "\">\n";
  print "<input type=\"hidden\" name=\"RestriccOperArit\" value=\"" . htmlentities(urlencode(serialize($_POST['RestriccOperArit']))) . "\">\n";
  print "<input type=\"hidden\" name=\"RestriccValor\" value=\"" . htmlentities(urlencode(serialize($_POST['RestriccValor']))) . "\">\n";
  print "<input type=\"hidden\" name=\"RestriccOperLogico\" value=\"" . htmlentities(urlencode(serialize($_POST['RestriccOperLogico']))) . "\">\n";

  print "<br>";
  $Query = "";
  $PseudoQuery = "";
  
  
  
  asort ($RestriccTabla);
  $registro_anterior = "";
  $datos_reg_ant = "";
  $ii=1;
  $n_registros = count($RestriccTabla) - 1;
  $tabla_campo_ant = "";
  $strROpLog_ant = "";
//  $ii = 0;
  $PseudoQuery_datos = "";
  
  
  
  
  
  for ($i = 0; $i < count($RestriccTabla); $i++) {
    $Tabla = $RestriccTabla[$i];
    $Campo = $RestriccCampo[$i];

    $ROpAr = $RestriccOperArit[$i];
    $RVal0 = $RestriccValor[$i][0];
    $RVal1 = $RestriccValor[$i][1];
    $ROpLog = $RestriccOperLogico[$i];

    if (strpos ($Tabla, "Encuestas#") > 0){
      $FldType = $CamposType[$Tabla][($Campo - 1)];
      $tabla_campo = $CamposDesc[$Tabla][$Campo - 1];
    }
    else {
      $FldType = $CamposType[$Tabla][$Campo];
      $tabla_campo = $CamposDesc[$Tabla][$Campo];
    }


    $strROpLog = "";
    if (Trim($ROpLog) == "NotSelected") $strROpLog = "y";
    if (Trim($ROpLog) == "and") $strROpLog = "y";
    if (Trim($ROpLog) == "or") $strROpLog = "o";


    if ($n_registros == 0){
      $PseudoQuery .= $tabla_campo . "<b>" . $strROpLog . "</b> " . PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1) . " <br>\n";
    }
    else if ($n_registros > 0){
      if ($tabla_campo == $tabla_campo_ant){
        if ($i == $n_registros) {
          $PseudoQuery .= " (" . $PseudoQuery_datos . " <b> " . $strROpLog_ant . " </b> " . PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1) . " )<br>";
        }
        else {
          $PseudoQuery_datos .= " <b> " . $strROpLog_ant . " </b> " . PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1) . "\n";
          $iii++;
        }
      }
      else {
        if ($i == $n_registros) {
          if ($iii > 1){
            $PseudoQuery .= " (" . $PseudoQuery_datos . ")<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>" . $strROpLog_ant . "<br></b>". $tabla_campo . "<b>" . $strROpLog . "</b> " . PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1) . "<br>";
          }
          else  {
            $PseudoQuery .= $PseudoQuery_datos . "<br>";
            $PseudoQuery .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>" . $strROpLog_ant . "</b><br>\n";
            $PseudoQuery .= $tabla_campo . " \n";
            $PseudoQuery .= PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1) . "<br>";
          }
        }
        else if ($i == 0){
          $PseudoQuery .= $tabla_campo . " \n";
          $PseudoQuery_datos = PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1);
          $iii = 1;
        }
        else if ($i > 0){
          if ($iii > 1){
             $PseudoQuery .= " (" . $PseudoQuery_datos . " )<br>";
          }
          else {
            $PseudoQuery .= $PseudoQuery_datos . "<br>";
          }
          $PseudoQuery .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>" . $strROpLog_ant . "</b><br>\n";
          $PseudoQuery .= $tabla_campo ;
          $PseudoQuery_datos = PseudoQuery($FldType, $ROpAr, $RVal0, $RVal1);
          $iii = 1;
        }
      }
    }
    $strROpLog_ant = $strROpLog;

    $tabla_campo_ant = $tabla_campo;
//    $Query .= GetQueryCondition($Tabla, $Campo, $FldType, $ROpAr, $RVal0, $RVal1) . " $ROpLog ";


// inicio del arreglo de las codicones del where agrupando los registros repetidos
  $_Q = GetQueryCondition($Tabla, $Campo, $FldType, $ROpAr, $RVal0, $RVal1) . " $ROpLog ";
  $_Q = str_replace ("tbl_UsuariosWeb.id_TipoTelefono", "tbl_TelefonosUsuario.id_TipoTelefono ",$_Q);
  
  
//  print $Tabla . "<br>" .  $Campo . "<br>" . $FldType . "<br>" .  $ROpAr . "<br>" .  $RVal0 . "<br>" . $RVal1 . "<br><br>";
  
  
  
  if ($Tabla == "tbl_ConfirmacionesEventosUsuariosWeb") {
    $_Q = str_replace ("=", " and i_Confirmado = ", $_Q);
    $_Q = str_replace (":", " = ", $_Q);
  }
  $r_a  = $Tabla . "-" . $Campo;
  if ($n_registros == 0){
    $datos .= $_Q;
  }
  else if ($n_registros > 0){
    if ($i == 0){
      $datos_reg_ant = $_Q;
    }
    else if (($i == $n_registros) and ($registro_anterior == $r_a)){
      $datos .= "(" . $datos_reg_ant . $_Q . ")";
    }
    else if (($i == $n_registros) and ($ii > 1) and ($registro_anterior <> $r_a) ){
      $fin = strrpos ($datos_reg_ant, ")");
      $_operador = substr ($datos_reg_ant, $fin, strlen($datos_reg_ant));
      $datos_reg_ant = substr($datos_reg_ant, 0, $fin);
      $datos .= "(" . $datos_reg_ant . ") " . $_operador . $_Q;
      $ii = 1;
    }
    else if (($i == $n_registros) and ($ii == 1) and ($registro_anterior <> $r_a)){
      $datos .= $datos_reg_ant . $_Q;
    }
    else if (($i > 0) and ($registro_anterior == $r_a)){
        $datos_reg_ant .= $_Q;
        $ii++;
    }
    else if (($i > 0) and ($ii > 1) and ($registro_anterior <> $r_a) ){
      $fin = strrpos ($datos_reg_ant, ")");
      $_operador = substr ($datos_reg_ant, $fin, strlen($datos_reg_ant));
      $datos_reg_ant = substr($datos_reg_ant, 0, $fin);
      $datos .= "(" . $datos_reg_ant . ") " . $_operador;
      $datos_reg_ant = $_Q;
      $ii = 1;
    }
    else if (($i > 0) and ($ii == 1) and ($registro_anterior <> $r_a)){
      $datos .= $datos_reg_ant;
      $datos_reg_ant = $_Q;
      $ii = 1;
    }
  }
    
    $registro_anterior = $r_a;
// fin del arreglo de las codicones del where agrupando los registros repetidos
  }

  $Query = $datos;
  print $PseudoQuery  . "\n";


  $Query = sprintf($cteQuerySegmentos, $Query);
  
  

  //print $Query . "<br>";


  $result = mssql_query ("select count(*) as NumReg from (" .$Query . ") as t1");
  $row = mssql_fetch_object($result);
  $Registros = $row->NumReg;

  
  print "<input type=\"hidden\" name=\"PseudoQuery\" value=\"" . htmlentities(urlencode($PseudoQuery)) . "\">\n";
  print "<input type=\"hidden\" name=\"Query\" value=\"" . htmlentities(urlencode($Query)) . "\">";

?>

                                        </td></tr>
                                        <tr class="answerCellBG"><td align="right">Tiene un total de <strong><? print $Registros ?></strong> registros</td></tr>
                                </table>
                                </td></tr>
                                </table>
                        </td></tr>
                        <tr><td><img src="../images/spacer.gif" height="10" width="1" alt=""></td></tr>
                        <tr><td>
                                <table border="0" cellpadding="5" cellspacing="1" class="mainTableBG">
                                        <tr class="answerCellBG">
                                                <td><img src="../images/icArrowGo.gif" height="13" width="13" alt=""></td>
                                                <td><a href="javascript:document.forms[0].submit();">Guardar y continuar al paso 5</a></td>
                                                <td><img src="../images/icArrowGo.gif" height="13" width="13" alt=""></td>
                                                <td><a href="javascript:if (confirm('Esta seguro que desea cancelar esta operaci&oacute;n?')){alert('Se cancelo con &eacute;xito la operaci&oacute;n');document.location = 'segmento-admin.php';}">Cancelar</a></td>
                                        </tr>
                                </table>
                        </td></tr>
                </table>
        </td>
        </tr>
</table>


          </td>
          <!-- <td width="36" background="../images/f_int.jpg"><img src="../images/f_int.jpg" width="36" height="126"></td> -->
        </tr>
      </table></td>
  </tr>
</table>
<!-- <table width="100%" border="0" cellpadding="0" cellspacing="0" background="../images/fotterint.jpg">
  <tr>
    <td><table width="800" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><img src="../images/f_int_new.jpg" height="32" width="800" alt=""></td>
        </tr>
      </table></td>
  </tr>
</table> -->



</form>
<br>
<br>
<br>
<table width="750" border="0" cellpadding="5" cellspacing="0">
  <tr>
    <td align="right"><span class="copyright"><?= $copyright ?></span></td>
  </tr>
</table>
</body>
</html>
